home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / misc / show1.0.lha / show / RCS / show.c,v < prev    next >
Encoding:
Text File  |  1994-12-07  |  9.0 KB  |  380 lines

  1. head    1.43;
  2. access;
  3. symbols
  4.     fontscreen:1.41
  5.     good:1.41;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.43
  11. date    94.12.05.03.32.28;    author jsshephe;    state Exp;
  12. branches;
  13. next    ;
  14.  
  15.  
  16. desc
  17. @original
  18. @
  19.  
  20.  
  21. 1.43
  22. log
  23. @Put up a file requester if the show icon was launched from Workbench with no other selected files.
  24. @
  25. text
  26. @/* Show by Jeff Shepherd
  27.  * Compiled using GCC2.6.0 and my makemake program
  28.  * Uses libnix. ie NO ixemul.library
  29.  * This allows the inclusion of the WBStartup message
  30.  */
  31.  
  32. /*
  33.  * $Revision: 1.42 $
  34.  * $Id: show.c,v 1.42 1994/12/04 23:30:45 jsshephe Exp jsshephe $
  35.  * $Log: show.c,v $
  36.  * Revision 1.42  1994/12/04  23:30:45    jsshephe
  37.  * Added inclusion of libraries/gadtools.h
  38.  *
  39.  * Revision 1.41  1994/11/27  21:58:46    jsshephe
  40.  * Changed the calls to Error() sinced it was changed in misc.c.
  41.  *
  42.  * Revision 1.40  1994/11/27  04:03:04    jsshephe
  43.  * Now prints usage if the command line is invalid (CLI only).
  44.  * Process tooltypes for SHOWSUFFIX for workbench use.
  45.  *
  46.  * Revision 1.30  1994/08/19  19:39:48    jsshephe
  47.  * Made show look for any suffix, not just those starting with '.'
  48.  *
  49.  * Revision 1.20  1994/08/18  21:29:57    jsshephe
  50.  * Can now load preferences file
  51.  * Can now do asynchonous commands by "run >NIL: command filename"
  52.  *
  53.  * Revision 1.11  1994/08/12  03:25:56    jsshephe
  54.  * added -f option to change default suffix recognition
  55.  *
  56.  * Revision 1.1  1994/08/12  03:02:56  jsshephe
  57.  * Initial revision
  58.  *
  59.  */
  60.  
  61. #include <exec/types.h>
  62. #include <exec/memory.h>
  63. #include <libraries/gadtools.h>
  64. #include <libraries/asl.h>
  65. #include <dos/dostags.h>
  66. #include <workbench/startup.h>
  67. #include <workbench/workbench.h>
  68. #include <string.h>
  69. #include <stdlib.h>
  70. #include <stdio.h>
  71. #include "showprefs.h"
  72.  
  73. #include <clib/alib_protos.h>
  74. #include <proto/exec.h>
  75. #include <proto/dos.h>
  76. #include <proto/icon.h>
  77. #include <proto/asl.h>
  78.  
  79. #define DIVIDE_STRING ";;" /* string that separates multiple commands */
  80. #define DIVIDE_STRING_LEN 2 /* strlen(DIVIDE_STRING) */
  81.  
  82. /* protos */
  83. LONG ExecuteCommand(char *, char *, BOOL);
  84. void panic(char *, int);
  85. void ParseWB(struct WBStartup *);
  86. void ParseCommand(int, char **);
  87. void FileSelect(void);
  88. void WBExecuteCommand(char *);
  89.  
  90. /* version string */
  91. char version[] = "\0$VER: show version 1.0";
  92.  
  93. struct Library *IFFParseBase = NULL, *IconBase = NULL, *AslBase = NULL;
  94. struct List *Suffix_List;
  95.  
  96. #ifdef __GNUC__
  97. extern struct WBStartup *_WBenchMsg;
  98. char __stdiowin[] = "KCON:0/150//230/Show/CLOSE/WAIT/AUTO";
  99. #endif
  100.  
  101. int main(int argc, char **argv) {
  102.  
  103.     if ( !(IFFParseBase = OpenLibrary("iffparse.library",37)))
  104.     panic ("Cannot open iffparse library version 37",1);
  105.  
  106.     if ( !(IconBase = OpenLibrary("icon.library",37)))
  107.     panic ("Cannot open icon.library version 37",2);
  108.  
  109.     if ( !(AslBase = OpenLibrary("asl.library",37)))
  110.     panic ("Cannot open asl.library version 37",3);
  111.  
  112.     if ( !(Suffix_List = AllocMem(sizeof(struct List),MEMF_PUBLIC|MEMF_CLEAR)))
  113.     panic("Cannot allocate list",3);
  114.  
  115.     NewList(Suffix_List);
  116.     Load("env:show.prefs",FALSE,NULL);
  117.  
  118. #ifdef __GNUC__
  119.     if (!_WBenchMsg) {
  120. #else
  121.     if (!argc) {
  122. #endif
  123.     /* came from CLI */
  124.     ParseCommand(argc, argv);
  125.     }
  126.     else {
  127.     /* came from WB */
  128. #ifdef __GNUC__
  129.     ParseWB(_WBenchMsg);
  130. #else
  131.     ParseWB((struct WBStartup *)argv);
  132. #endif
  133.     }
  134.     panic(NULL,0);
  135. }
  136.  
  137. /* execute command with filename tagged on the end */
  138. /* As of now, Asynch does puts a "run >NIL:" in front of the command */
  139. LONG ExecuteCommand(char *command, char *filename, BOOL Asynch) {
  140.     LONG retval;
  141.     BOOL found_divide_char = FALSE;
  142.     char *commandline = malloc(strlen(command) + strlen(filename)+12);
  143.     char *end_of_command;
  144.     char *exec_command;
  145.     char *the_command;
  146.  
  147.     #ifdef debug
  148.     printf("first command %s, filename %s\n",command,filename);
  149.     #endif
  150.     if (Asynch) {
  151.     strcpy(commandline,"run >NIL: ");
  152.     strcat(commandline,command);
  153.     }
  154.     else {
  155.     strcpy(commandline,command);
  156.     }
  157.     the_command = commandline;
  158.  
  159.     #ifdef debug
  160.     printf("Whole commandline %s\n",commandline);
  161.     #endif
  162.  
  163.     do {
  164.     end_of_command = strstr(the_command,DIVIDE_STRING);
  165.  
  166.     if (end_of_command != NULL) {
  167.         *end_of_command = '\0';
  168.         found_divide_char = TRUE;
  169.     }
  170.     else if (found_divide_char == FALSE) {
  171.         strcat(commandline," ");
  172.         strcat(commandline,filename);
  173.     }
  174.  
  175.     #ifdef debug
  176.         printf("before command: %s\n", the_command);
  177.     #endif
  178.  
  179.     /* fill in %s if any */
  180.     if (strstr(the_command,"\%s") != NULL) {
  181.         exec_command = malloc(strlen(the_command) + strlen(filename)-2);
  182.         strcpy(exec_command,the_command);
  183.         sprintf(exec_command,the_command,filename);
  184.  
  185.         #ifdef debug
  186.         printf("after command: %s\n", exec_command);
  187.         #endif
  188.  
  189.         retval=SystemTags(exec_command, TAG_END);
  190.         free(exec_command);
  191.     } /* if */
  192.     else {
  193.         #ifdef debug
  194.         printf("after command: %s\n", the_command);
  195.         #endif
  196.         retval=SystemTags(the_command, TAG_END);
  197.     }
  198.  
  199.     the_command = end_of_command + DIVIDE_STRING_LEN;
  200.  
  201.     } while (end_of_command != NULL);
  202.  
  203.     free(commandline);
  204.     return retval;
  205. }
  206.  
  207.  
  208. /* cleanup time */
  209. void panic(char *error, int retval) {
  210.     if (error)
  211.     fprintf(stderr,"ERROR: %s\n",error);
  212.  
  213.     if (Suffix_List) {
  214.     Destroy_List(Suffix_List);
  215.     FreeMem(Suffix_List,sizeof(struct List));
  216.     }
  217.  
  218.     if (AslBase)
  219.     CloseLibrary(AslBase);
  220.  
  221.     if (IconBase)
  222.     CloseLibrary(IconBase);
  223.  
  224.     if (IFFParseBase)
  225.     CloseLibrary(IFFParseBase);
  226.  
  227.     exit(retval);
  228. }
  229.  
  230. void ParseCommand(int argc, char **argv) {
  231.     int j;
  232.     char error[MAX_LENGTH];
  233.     struct SuffixList *SNode;
  234.  
  235.     if (argc < 2) {
  236.     printf("USAGE: %s [-f suffix] filename [[-f suffix]filename2...]\n",argv[0]);
  237.     exit(2);
  238.     }
  239.  
  240.     for (j=1; j < argc; j++) {
  241.     if (*argv[j] == '-') {
  242.  
  243.         /* I use a switch statement to allow further expansion */
  244.         switch (argv[j][1]) {
  245.  
  246.         /* change default handling of suffix */
  247.         case 'f':
  248.             if (argv[j][2]) {
  249.             SNode = (struct SuffixList *)FindName(Suffix_List,&argv[j][2]);
  250.             }
  251.             else {
  252.             if (++j >= argc) panic("Bad command line",2);
  253.             SNode = (struct SuffixList *)FindName(Suffix_List,argv[j]);
  254.             }
  255.             j++;
  256.             if (j >= argc)
  257.             panic("Bad command line",2);
  258.  
  259.             /* Make sure the user specified a suffix that is defined */
  260.             if (SNode) {
  261.             ExecuteCommand(SNode->command, argv[j], SNode->Asynch);
  262.             }
  263.             else {
  264.             sprintf(error,"suffix in %s not found",argv[j]);
  265.             panic(error,3);
  266.             } /* if */
  267.         break;
  268.  
  269.         } /* switch */
  270.     } /* if */
  271.     else {
  272.         if (SNode = (struct SuffixList *)SearchSuffix(Suffix_List,argv[j])) {
  273.         ExecuteCommand(SNode->command, argv[j], SNode->Asynch);
  274.         }
  275.         else {
  276.         sprintf(error,"Suffix in %s not defined",argv[j]);
  277.         panic(error,4);
  278.         }/* if */
  279.     } /* else */
  280.     } /* for */
  281. } /* ParseCommand() */
  282.  
  283. /* parse workbench startup message */
  284. void ParseWB(struct WBStartup *WBMsg) {
  285.     struct WBArg *args = WBMsg->sm_ArgList;
  286.     int i;
  287.     char dir[2*MAX_LENGTH];
  288.  
  289.     /* pass over the argument for "show" */
  290.     args++;
  291.  
  292.     /* bring up ASL Requester if just clicked on icon */
  293.     if (WBMsg->sm_NumArgs == 1) {
  294.     FileSelect();
  295.     }
  296.     else {
  297.  
  298.     /* go over the passed in files and "show" them */
  299.     for(i=1; i < WBMsg->sm_NumArgs; i++, args++) {
  300.         /* get absolute path for filename */
  301.         NameFromLock(args->wa_Lock,dir,MAX_LENGTH);
  302.         AddPart(dir,args->wa_Name,MAX_LENGTH);
  303.         WBExecuteCommand(dir);
  304.     } /* for */
  305.     } /* if */
  306. }
  307.  
  308. void FileSelect(void) {
  309.     struct FileRequester *fr;
  310.     struct WBArg *args;
  311.     int i;
  312.     char dir[MAX_LENGTH];
  313.  
  314.     if (!(fr = AllocAslRequestTags(ASL_FileRequest,
  315.     ASLFR_TitleText, "Select File...",
  316.     ASLFR_PositiveText, "Show",
  317.     ASLFR_Flags1, FRF_DOMULTISELECT,
  318.     ASLFR_Flags2, FRF_REJECTICONS,
  319.     TAG_DONE ))) {
  320.     Error("Cannot allocate ASL requester");
  321.     }
  322.  
  323.     while (AslRequest(fr,NULL)) {
  324.     if (fr->rf_NumArgs) {
  325.         for (i=0, args = fr->rf_ArgList; i < fr->rf_NumArgs; i++,args++) {
  326.         NameFromLock(args->wa_Lock,dir,MAX_LENGTH);
  327.         AddPart(dir,args->wa_Name,MAX_LENGTH);
  328.         WBExecuteCommand(dir);
  329.         } /* for */
  330.     } /* if */
  331.     else {
  332.         strcpy(dir,fr->rf_Dir);
  333.         AddPart(dir,fr->rf_File,MAX_LENGTH);
  334.         WBExecuteCommand(dir);
  335.     } /* if */
  336.     } /* if */
  337.     FreeAslRequest(fr);
  338. }
  339.  
  340. /* Executes the command by first search for the tooltype array then its suffix */
  341. void WBExecuteCommand(char *dir) {
  342.     struct DiskObject *dkobj;
  343.     char *tooltype_suffix;
  344.     char *filename = FilePart(dir);
  345.     BOOL Executed_Command = FALSE; /* used just in case tooltype was not found */
  346.     static char error[MAX_LENGTH];
  347.     struct SuffixList *SNode;
  348.  
  349.     if ((dkobj=GetDiskObject(dir)) != NULL) {
  350.     if ((tooltype_suffix = FindToolType(dkobj->do_ToolTypes,"SHOWSUFFIX"))) {
  351.         Executed_Command = TRUE;
  352.         if (SNode = (struct SuffixList *)FindName(Suffix_List, tooltype_suffix)) {
  353.         ExecuteCommand(SNode->command,dir,SNode->Asynch);
  354.         } /* if */
  355.         else {
  356.         sprintf(error,"Suffix in tooltypes of file %s not defined.",filename);
  357.         Error(error);
  358.         } /* if */
  359.     } /* if */
  360.     FreeDiskObject(dkobj);
  361.     } /* if */
  362.  
  363.     /* just in case tooltype wasn't found, execute suffix search */
  364.     if (Executed_Command == FALSE) {
  365.     if (SNode = (struct SuffixList *)SearchSuffix(Suffix_List,filename)) {
  366.         ExecuteCommand(SNode->command,dir,SNode->Asynch);
  367.     }
  368.     else {
  369.         sprintf(error,"Suffix in %s not defined.",filename);
  370.         Error(error);
  371.     }
  372.     } /* if */
  373. }
  374.  
  375.  
  376.  
  377.  
  378.  
  379. @
  380.